package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsTempWindow;
import com.zhike.service.DwsTempWindowService;
import com.zhike.mapper.DwsTempWindowMapper;
import com.zhike.utils.BarUtils;
import com.zhike.utils.EmailUtils;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_temp_window(车辆温度信息表)】的数据库操作Service实现
* @createDate 2024-05-01 21:05:55
*/
@Service
public class DwsTempWindowServiceImpl extends ServiceImpl<DwsTempWindowMapper, DwsTempWindow>
    implements DwsTempWindowService{

    @Autowired
    private DwsTempWindowMapper dwsTempWindowMapper;
    @Override
    public String getTempToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempToday(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_temp_motor","max_temp_motor","电机平均温度","电机最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getTempConToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempConToday(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_temp_motor_controller","max_temp_motor_controller","控制器平均温度","控制器最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getTempCountToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempCountToday(date);
            if (!avgSpeedToday.isEmpty()) {
                for (Map map : avgSpeedToday) {
                    Double maxTempBattery = Double.parseDouble(map.get("max_temp_battery").toString());
                    if (maxTempBattery > 50){
                        String vin = map.get("vin").toString();
                        EmailUtils.sendEmail(vin);
                    }
                }
            }
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","battery_temp_abnormal_count","max_temp_battery","电池异常次数","电池最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getTempWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_temp_motor","max_temp_motor","电机平均温度","电机最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getTempConWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempConWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_temp_motor_controller","max_temp_motor_controller","控制器平均温度","控制器最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getTempCountWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTempWindowMapper.getTempCountWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","battery_temp_abnormal_count","max_temp_battery","电池异常次数","电池最高温度");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }
}




